package com.example.base_kotlin_xml

import android.os.Bundle
import androidx.activity.viewModels
import androidx.annotation.IdRes
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import com.blankj.utilcode.util.LogUtils
import com.example.base_kotlin_xml.databinding.ActivityMainBinding
import com.example.base_model.socket.ModernMqttManager
import com.example.base_model.socket.MqttConfig
import com.example.base_model.view.BaseActivity
import dagger.hilt.android.AndroidEntryPoint
import jakarta.inject.Inject

/**
 * @Author: jiangKunKun
 * @CreateDate: 2025/6/20
 * @Description:
 * @SpittingGrooves
 */
@AndroidEntryPoint
class MainActivity() : BaseActivity<ActivityMainBinding>() {

    val vModel: MainViewModel by viewModels()
    // 通过Hilt注入MQTT管理器实例
    @Inject
    lateinit var mqttManager: ModernMqttManager

    override fun initObservers() {
    }


    override fun beforeSetContent() {
    }

    override fun initData() {
        // 创建MQTT配置
        val config = MqttConfig(
            serverUri = "tcp://test-mqtt.tianjinchuxing.com:1883",
            username = "tjcd",
            password = "CGQZ.d2fdD",
            clientId = "jkk_android_client_${System.currentTimeMillis()}"
        )

        // 初始化MQTT管理器
        mqttManager.initialize(config)
    }

    override fun onResume() {
        super.onResume()
    }


    override fun initClickEvents() {

    }


    override fun attachLayoutRes(): Int = R.layout.activity_main

    override fun onDestroy() {
        super.onDestroy()

    }

    /**
     * 导航到登录页面并清理栈
     * @param isForceLogin 是否为强制登录，true时登录后返回主页，false时返回原页面
     * @param destinationId 要弹出到的目标ID
     *   - 当传入 navController.graph.id 时，表示弹出到导航图的根节点
     *   - 当传入具体Fragment ID时，表示弹出到指定Fragment
     *
     * @param inclusive 是否包含目标本身
     *   - false: 弹出到目标Fragment为止，但保留目标Fragment在栈中
     *   - true: 弹出目标Fragment及其之上的所有Fragment
     *
     * 例如：
     *   setPopUpTo(R.id.homeFragment, inclusive = false)
     *   -> 保留homeFragment，移除其上的所有Fragment
     *
     *   setPopUpTo(R.id.homeFragment, inclusive = true)
     *   -> 移除homeFragment及其之上的所有Fragment
     */
    fun navigateToLoginAndClearStack(isForceLogin: Boolean = false, @IdRes navHostId: Int = R.id.nav_host_fragment) {
        try {
            val navController = findNavController(navHostId)
            val navOptions = NavOptions.Builder()
                .setPopUpTo(navController.graph.id, inclusive = false)
                .setEnterAnim(com.example.base_model.R.anim.slide_in_left)
                .setExitAnim(com.example.base_model.R.anim.slide_out_left)
                .setPopEnterAnim(com.example.base_model.R.anim.slide_in_left)
                .setPopExitAnim(com.example.base_model.R.anim.slide_out_right)
                .build()

            // 通过参数传递是否强制登录的标识
            val args = Bundle().apply {
                putBoolean("isForceLogin", isForceLogin)
            }
            // 确保当前目的地有效后再导航
            if (navController.currentDestination != null) {
                navController.navigate(R.id.loginFragment, args, navOptions)
            }
        } catch (e: Exception) {
            LogUtils.e("导航到登录页面失败", e.message)
        }
    }

}